package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.MonitorLifeDataDao;
import com.jyxd.web.data.basic.MonitorLifeData;
import com.jyxd.web.data.basic.NurseRecordForm;
import com.jyxd.web.data.basic.VitalSignMerge;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.data.patient.Patient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.jyxd.web.util.DateUtil.*;
import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;
import static com.jyxd.web.util.ExecuteUtil.isNotEmpty;
import static com.jyxd.web.util.VitalSignUtil.getIsOpenPulseRecord;

@Service
@Transactional
public class MonitorLifeDataService {

    @Autowired
    private MonitorLifeDataDao monitorLifeDataDao;

    public HttpResult getDataByTime(Map<String, Object> map) {
        errorIfEmpty(map, "查询参数不存在");
        errorIfEmpty(map.get("patientId"), "查询患者id不存在");
        MonitorLifeData monitorLifeData = new MonitorLifeData();
        monitorLifeData.setStatus("1");
        monitorLifeData.setPatientId((String) map.get("patientId"));
        if (map.containsKey("time") && isNotEmpty(map.get("time")))
            monitorLifeData.setDateTime((String) map.get("time"));
        monitorLifeData = monitorLifeDataDao.queryListByTime(monitorLifeData);
        return Objects.isNull(monitorLifeData) ?
                new HttpResult(200, "暂无数据") :
                new HttpResult(200, monitorLifeData, "查询成功");
    }

    public boolean insert(MonitorLifeData monitorData) {
        return monitorLifeDataDao.insert(monitorData);
    }

    public MonitorLifeData queryData(String id) {
        return monitorLifeDataDao.queryData(id);
    }

    public HttpResult queryList(MonitorLifeData monitorLifeData) {
        List<MonitorLifeData> monitorLifeDataList = monitorLifeDataDao.queryList(monitorLifeData);
        if (CollectionUtils.isEmpty(monitorLifeDataList))
            return new HttpResult(200, "暂无数据");
        else
            return new HttpResult(200, monitorLifeDataList, "请求成功");
    }

    /**
     * 查询患者监护仪数据最新的
     *
     * @param monitorLifeData
     * @return
     */
    public HttpResult queryNewestList(MonitorLifeData monitorLifeData) {
        List<MonitorLifeData> monitorLifeDataList = monitorLifeDataDao.queryList(monitorLifeData);
        if (CollectionUtils.isEmpty(monitorLifeDataList))
            return new HttpResult(200, "暂无数据");
        else
            return new HttpResult(200, monitorLifeDataList.get(0), "请求成功");
    }

    public int queryNum(Map<String, Object> map) {
        return monitorLifeDataDao.queryNum(map);
    }

    public boolean deleteData(Map<String, Object> map) {
        return monitorLifeDataDao.deleteData(map);
    }

    /**
     * 查询最新一条血压数据
     *
     * @param
     * @return MonitorData
     */
    public MonitorLifeData queryNewsNibpsData(Map<String, Object> map) {
        return monitorLifeDataDao.queryNewsNibpsData(map);
    }

    public Object setObjectMonitorLifeData(Patient patient, String time, Object object) {
        if (Objects.isNull(patient))
            return object;

        MonitorLifeData queryMonitorLifeData = new MonitorLifeData();
        queryMonitorLifeData.setDateTime(time);
        queryMonitorLifeData.setPatientId(patient.getId());
        List<MonitorLifeData> monitorLifeDataList = monitorLifeDataDao.queryList(queryMonitorLifeData);
        if (CollectionUtils.isEmpty(monitorLifeDataList)) {
            queryMonitorLifeData.setDateTime(null);
            queryMonitorLifeData.setDateEndTime(time);
            Date date = null;
            try {
                date = yyyyMMddHHmmSdfToDate(time);
                Date startTime = getLaterMinuteDate(date, -30L);
                queryMonitorLifeData.setDateStartTime(yyyyMMddHHmmssSdfToString(startTime));
            } catch (ParseException e) {
            }
            monitorLifeDataList = monitorLifeDataDao.queryList(queryMonitorLifeData);
        }

        if(CollectionUtils.isEmpty(monitorLifeDataList)){
            return object;
        }

        MonitorLifeData monitorLifeData = monitorLifeDataList.get(0);

        if (Objects.isNull(monitorLifeData))
            return object;
        if (Objects.isNull(object))
            return object;
        if (object instanceof VitalSignMerge) {
            VitalSignMerge vitalSign = (VitalSignMerge) object;
            vitalSign.setTemplate(setValue(vitalSign.getTemplate(), monitorLifeData.getTemp()));//体温
            vitalSign.setHr(setValue(vitalSign.getHr(), monitorLifeData.getHr()));//心率
            vitalSign.setBr(setValue(vitalSign.getBr(), monitorLifeData.getResp()));//呼吸
            if (vitalSign.getPulse() == null && getIsOpenPulseRecord(patient))  // 判断脉率为空 且 脉率状态为开启
                vitalSign.setPulse(monitorLifeData.getPulse());//脉率
            String arts = monitorLifeData.getArts();
            vitalSign.setAbp(setBloodValue(vitalSign.getAbp(),
                    monitorLifeData.getArts() + "/" + monitorLifeData.getArtd(), arts)); //有创血压
            String nibps = monitorLifeData.getNibps();
            vitalSign.setNbp(setBloodValue(vitalSign.getNbp(),
                    monitorLifeData.getNibps() + "/" + monitorLifeData.getNibpd(), nibps)); //有创血压
            return vitalSign;
        }
        if (object instanceof NurseRecordForm) {
            NurseRecordForm nurseRecordForm = (NurseRecordForm) object;
            if (nurseRecordForm.getTemplate() == null)
                nurseRecordForm.setTemplate(monitorLifeData.getTemp());//体温
            if (nurseRecordForm.getHr() == null)
                nurseRecordForm.setHr(monitorLifeData.getHr());//心率
            if (nurseRecordForm.getPulse() == null && getIsOpenPulseRecord(patient))  // 判断脉率为空 且 脉率状态为开启
                nurseRecordForm.setPulse(monitorLifeData.getPulse());//脉率
            if (nurseRecordForm.getBr() == null)
                nurseRecordForm.setBr(monitorLifeData.getResp());//呼吸
            String arts = monitorLifeData.getArts();
            if (nurseRecordForm.getAbp() == null && StringUtils.isNotEmpty(arts) && !arts.equals("null"))
                nurseRecordForm.setAbp(monitorLifeData.getArts() + "/" + monitorLifeData.getArtd());//有创血压
            String nibps = monitorLifeData.getNibps();
            if (nurseRecordForm.getNbp() == null && StringUtils.isNotEmpty(nibps) && !nibps.equals("null"))
                nurseRecordForm.setNbp(monitorLifeData.getNibps() + "/" + monitorLifeData.getNibpd());//无创血压
            return nurseRecordForm;
        }
        return object;
    }

    private String setValue(String oldValue, String newValue) {
        if (StringUtils.isEmpty(oldValue))
            return newValue;
        return oldValue;
    }

    private String setBloodValue(String oldValue, String newValue, String isStartValue) {
        if (StringUtils.isEmpty(oldValue) && StringUtils.isNotEmpty(isStartValue) && !isStartValue.equals("null"))
            return newValue;
        return oldValue;
    }

}

